﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 数据库索引
    /// </summary>
    [Serializable]
    public class DataObjectIndex : System.ICloneable
    {
        /// <summary>
        /// 主键guid
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 唯一标识
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 索引所在DO的标识
        /// </summary>
        [JsonIgnore]
        public string DataObjectID { get; set; }

        /// <summary>
        /// 是否聚集索引
        /// </summary>
        public bool IsClustered { get; set; }

        /// <summary>
        /// 是否唯一索引
        /// </summary>
        public bool IsUnique { get; set; }

        /// <summary>
        /// 数据库类型 SQLServer;MySQL
        /// </summary>
        public string DbType { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        /// <summary>
        /// 索引列
        /// </summary>
        public List<DataObjectIndexColumn> Columns { get; set; } = new List<DataObjectIndexColumn>();

        /// <summary>
        /// 包含列
        /// </summary>
        public List<DataObjectIndexColumn> IncludeColumns { get; set; } = new List<DataObjectIndexColumn>();


        public object Clone()
        {
            var newObject = this.MemberwiseClone() as DataObjectIndex;
            if (Columns != null)
            {
                newObject.Columns = new List<DataObjectIndexColumn>();
                this.Columns.ForEach(i => newObject.Columns.Add(i.Clone() as DataObjectIndexColumn));
            }
            if (IncludeColumns != null)
            {
                newObject.IncludeColumns = new List<DataObjectIndexColumn>();
                this.IncludeColumns.ForEach(i => newObject.IncludeColumns.Add(i.Clone() as DataObjectIndexColumn));
            }
            return newObject;
        }
    }
}
